package com.skywolf.chem.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.article.entity.Article;
import com.skywolf.chem.article.mapper.ArticleMapper;
import com.skywolf.chem.article.pojo.ArticleQueryParam;
import com.skywolf.chem.article.service.IArticleService;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帖子 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public PageVO<Article> pageArticleList(ArticleQueryParam queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        Integer count = 0;
        List<Article> articleList = new ArrayList<>();
        if(queryParam.getColId()!=null && queryParam.getColId()==-10000L){
            List<Long> articleId = articleMapper.getRecommend();
            if(articleId!=null){
                articleList = articleMapper.getRecommendList(articleId);
                count = articleList.size();
                queryParam.setPageSize(new Long(count));
            }
        }else{
            count = articleMapper.getArticlePageCount(queryParam.getColId(),queryParam.getTitleDesc(),
                    queryParam.getArtStatus(),queryParam.getArtType(),queryParam.getStartTime(),queryParam.getEndTime());
            articleList = articleMapper.getArticlePageList(startNum,endNum,
                    queryParam.getColId(),queryParam.getTitleDesc(),queryParam.getArtStatus(),
                    queryParam.getArtType(),queryParam.getStartTime(),queryParam.getEndTime());
        }
        PageVO<Article> pageVO = new PageVO<>();
        pageVO.setList(articleList);
        pageVO.setTotal(Long.valueOf(count));
        pageVO.setPageSize(queryParam.getPageSize());
        pageVO.setPageNum(queryParam.getPageNum());
        Long totalPage = (count/queryParam.getPageSize())+(count%queryParam.getPageSize()>0?1:0);
        if(totalPage.equals(0)){
            totalPage = queryParam.getPageNum();
        }
        pageVO.setPages(totalPage);
        if(queryParam.getColId()!=null && queryParam.getColId()==-10000L){
            pageVO.setPages(1L);
        }
        return pageVO;
    }

    @Override
    public PageVO<Article> pageArticleListManager(ArticleQueryParam queryParam,Long city) {
        if(city!=0){
            queryParam.setColId(city);
        }
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        Integer count = articleMapper.getArticlePageCountManager(queryParam.getColId(),queryParam.getTitleDesc(),
                queryParam.getArtStatus(),queryParam.getArtType(),queryParam.getStartTime(),queryParam.getEndTime(),queryParam.getPcfirst());
        List<Article> articleList = articleMapper.getArticlePageListManager(startNum,endNum,
                queryParam.getColId(),queryParam.getTitleDesc(),queryParam.getArtStatus(),
                queryParam.getArtType(),queryParam.getStartTime(),queryParam.getEndTime(),queryParam.getPcfirst());

        PageVO<Article> pageVO = new PageVO<>();
        pageVO.setList(articleList);
        pageVO.setTotal(Long.valueOf(count));
        pageVO.setPageSize(queryParam.getPageSize());
        pageVO.setPageNum(queryParam.getPageNum());
        Long totalPage = (count/queryParam.getPageSize())+(count%queryParam.getPageSize()>0?1:0);
        if(totalPage.equals(0)){
            totalPage = queryParam.getPageNum();
        }
        pageVO.setPages(totalPage);
        return pageVO;
    }

    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    @Override
    public void updateArticlebug(String opt,String msg) {
        List<Article> list = this.list();
        if(opt.equals("chuantibug-fix")){
            for (Article item:list) {
                String title = item.getTitle();
                title = title.replaceAll(msg,"");
                articleMapper.updateArticleBug(title,item.getId());
            }
        }
        if(opt.equals("chuantibug-add")){
            for (Article item:list) {
                String title = item.getTitle();
                title = msg+""+title;
                articleMapper.updateArticleBug(title,item.getId());
            }
        }
    }
}
